package ai

const (
	idle = iota
	attack
	back
)

var StateMap map[int32]StateInterface

func init() {
	StateMap = make(map[int32]StateInterface, 3)
	StateMap[idle] = new(MonsterIdleState)
	StateMap[attack] = new(MonsterAttackState)
	StateMap[back] = new(MonsterBackState)
}

type StateInterface interface {
	Enter()
	Exit()
	Update()
	CheckChange() StateInterface
}

type FsmCtl struct {
	CurrentState StateInterface
	DefaultState StateInterface
}
type PersonAiCtl struct {
	*FsmCtl
}
type PersonBehaviorStateCtl struct {
	AiState StateInterface
}

func NewPersonAiCtl() *PersonAiCtl {
	FsmCtl := &FsmCtl{StateMap[idle], StateMap[idle]}
	return &PersonAiCtl{FsmCtl}
}

func (fsm *FsmCtl) Update() {

	activeState := fsm.CurrentState.CheckChange()
	if activeState == nil {
		activeState = fsm.DefaultState
	}
	if fsm.CurrentState != activeState {
		fsm.CurrentState.Exit()
		activeState.Enter()
		fsm.CurrentState = activeState
	} else {
		fsm.CurrentState.Update()
	}
}
